<?php

namespace App\Http\Controllers\Act;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Repositories\Eloquent\Act\TopicRepository as Topic;
use App\Repositories\Eloquent\Act\QuestionRepository as Question;
/**
 * Class LoginController
 * @package App\Http\Backend\Controllers
 */
class QuestionController extends Controller
{
    /**
     * @var Administrator
     */
    protected $topic, $question;

    public function __construct(Topic $topic, Question $question)
    {
        parent::__construct();

        $this->topic = $topic;

        $this->question = $question;
    }


    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index($tid)
    {
        $topic = $this->topic->show($tid);

        if ($topic instanceof JsonResponse && $topic->getStatusCode() === 404) {
            return abort('404');
        }

        $questions = $topic->questions;

        $paging = round(count($questions)/2);

        return view($this->view, compact('topic','questions','paging'));
    }
    

    public function createSelect($tid)
    {
        return view($this->view,compact('tid'));
    }

    public function createBlank($tid)
    {
        return view($this->view,compact('tid'));
    }


    public function store(Request $request, $tid)
    {
        return $this->question->store($request, $tid);
    }


    public function editSelect($tid,$qid)
    {
        $question = $this->question->show($qid);

        if ($question instanceof JsonResponse && $question->getStatusCode() === 404) {
            return abort('404');
        }

        return view($this->view, compact('question'));
    }


    public function editBlank($tid,$qid)
    {
        $question = $this->question->show($qid);

        if ($question instanceof JsonResponse && $question->getStatusCode() === 404) {
            return abort('404');
        }

        return view($this->view, compact('question'));
    }
    

    public function update(Request $request, $tid, $qid)
    {
        return $this->question->renew($request, $tid, $qid);
    }


}
